package com.huifaapp.mall.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.ShopAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.ShopBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends Fragment {


    private RecyclerView shop_Xrecy;
    private ShopAdapter shopAdapter;
    private List<ShopBean.DataBean> goods;





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_shop,null);
        initView(view);


        return view;
    }

    private void initView(View view) {
        shop_Xrecy = (RecyclerView) view.findViewById(R.id.shop_xrecy);
        shop_Xrecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        Log.e("商品详情url------------",ApiManager.BASE_NIUPAI_URL+"users/mystorecollectlist?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(getActivity()));
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"users/mystorecollectlist")
                //token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    initData(response);
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        });


    }

    private void initData(String response) {
        Gson gson = new Gson();
        ShopBean shopBean = gson.fromJson(response,ShopBean.class);
        goods = shopBean.getData();
        shopAdapter = new ShopAdapter(getActivity(),goods);
        shop_Xrecy.setAdapter(shopAdapter);




    }

}